package com.sdry.service.impl.zc;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.zc.AbnormalDisposalCraftMapper;
import com.sdry.mapper.zc.AbnormalDisposalMapper;
import com.sdry.mapper.zc.ChangeNoticeMapper;
import com.sdry.model.zc.AbnormalDisposal;
import com.sdry.model.zc.AbnormalDisposalCraft;
import com.sdry.model.zc.ChangeNotice;
import com.sdry.service.zc.AbnormalDisposalCraftService;

/**
 * @ClassName: AbnormalDisposalCraftServiceImpl 
 * @Description:生产异常处置单工艺详情Service实现
 * @Author zc
 * @Date 2018年11月5日 下午12:02:27 
 * @version 1.0
 */
@Service
public class AbnormalDisposalCraftServiceImpl implements AbnormalDisposalCraftService {

	@Resource
	private AbnormalDisposalCraftMapper abnormalDisposalCraftMapper;
	@Resource
	private ChangeNoticeMapper changeNoticeMapper;
	@Resource
	private AbnormalDisposalMapper abnormalDisposalMapper;
	
	/**
	 * 查询生产异常处置单工艺详情
	 * @param abnormalDisposalCraft
	 * @return
	 */
	public AbnormalDisposalCraft findAbnormalDisposalCraftByAdID(AbnormalDisposalCraft abnormalDisposalCraft) {
		// TODO Auto-generated method stub
		return abnormalDisposalCraftMapper.findAbnormalDisposalCraftByAdID(abnormalDisposalCraft);
	}

	/**
	 * 新增生产异常处置单工艺详情
	 * @param abnormalDisposalCraft
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public Integer insertAbnormalDisposalCraft(AbnormalDisposalCraft abnormalDisposalCraft,ChangeNotice changeNotice) {
		// TODO Auto-generated method stub
		int isSuccess = 0;
		try {
			if (abnormalDisposalCraft.getIsNormal().equals("1")) {
				isSuccess = changeNoticeMapper.insertChangeNotice(changeNotice);
			}
			abnormalDisposalCraft.setNoticeID(Long.parseLong(isSuccess+""));
			isSuccess = abnormalDisposalCraftMapper.insertAbnormalDisposalCraft(abnormalDisposalCraft);
			AbnormalDisposal abnormalDisposal = abnormalDisposalMapper.findAbnormalDisposalByID(abnormalDisposalCraft.getAdID());
			abnormalDisposal.setIsNormal(abnormalDisposalCraft.getIsNormal());
			if ("0".equals(abnormalDisposalCraft.getReserve1())) {
				abnormalDisposal.setReserve1("1");
			} else {
				abnormalDisposal.setReserve1("-1");
			}
			isSuccess = abnormalDisposalMapper.setStatus(abnormalDisposal);
		} catch (Exception e) {
			e.printStackTrace();     
	        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	        isSuccess = 0;
		}
		return isSuccess;
	}

}
